package com.zhonghui.modlules.cart.controller;

import com.zhonghui.common.bean.LocalUser;
import com.zhonghui.common.eshopUtil.ApiResult;
import com.zhonghui.modlules.cart.eshopService.StoreCartService;
import com.zhonghui.modlules.cart.param.CartIdsParm;
import com.zhonghui.modlules.cart.param.CartNumParam;
import com.zhonghui.modlules.cart.param.CartParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@Api(value = "购物车",tags = "购物车")
public class StoreCartController {

    private final StoreCartService storeCartService;

    @ApiOperation(value = "添加购物车",notes = "添加购物车")
    @PostMapping("/cart/add")
    public ApiResult<Map<String,Object>> cartAdd(@Validated @RequestBody CartParam cartParam){
        Long uid = LocalUser.getUser().getUid();
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("cartId",storeCartService.addCart(uid,cartParam.getProductId(),cartParam.getCartNum(),
                cartParam.getUniqueId(),cartParam.getIsNew(),cartParam.getCombinationId(),cartParam.getSecKillId()
        ,cartParam.getBargainId()));

        return ApiResult.ok(map);
    }

    @ApiOperation(value = "删除购物车",notes = "删除购物车")
    @PostMapping("/cart/del")
    public ApiResult<Boolean> cartDel(@Validated @RequestBody CartIdsParm cartIdsParm){
        Long uid = LocalUser.getUser().getUid();
        storeCartService.removeUserCart(uid,cartIdsParm.getIds());
        return ApiResult.ok();
    }

    @ApiOperation(value = "修改数量",notes = "修改数量")
    @PostMapping("/cart/num")
    public ApiResult<Boolean> cartNum(@Validated @RequestBody CartNumParam cartNumParam){
        Long uid = LocalUser.getUser().getUid();
        storeCartService.changeUserCartNum(cartNumParam.getId(),cartNumParam.getNumber(),uid);
        return ApiResult.ok();
    }

    @ApiOperation(value = "查看购物车",notes = "查看购物车")
    @GetMapping("/cart/list")
    public ApiResult<Map<String,Object>> cartList(){
        Long uid = LocalUser.getUser().getUid();
        return ApiResult.ok(storeCartService.getUserProductCartList(uid,"",null));
    }

    @ApiOperation(value = "获取数量",notes = "获取数量")
    @GetMapping("/cart/count")
    public ApiResult<Map<String,Object>> cartCount(){
        Long uid = LocalUser.getUser().getUid();
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("count",storeCartService.getUserCartNum(uid));
        return ApiResult.ok(map);
    }
}
